/**
  ******************************************************************************
* @file				bsp_uart.c
* @Software			MDK V5
* @Target 			MM32F5330
* @author			ou_teki
* @date				2024-0715
  ******************************************************************************
  */


#include	"uart_com.h"

//-TKW24-0715-Uart Init
//-UART1_TX_PA9  UART1_RX_PA10 
uint8_t    UartComInit(void)
{
    //-TKW24-0715-Clock Config
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);

    //-TKW24-0715-GPIO Config
    GPIO_InitTypeDef gpio_uart_init_s;
    gpio_uart_init_s.GPIO_Speed = GPIO_Speed_High;
    gpio_uart_init_s.GPIO_Pin = GPIO_Pin_9;
    gpio_uart_init_s.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &gpio_uart_init_s);
    gpio_uart_init_s.GPIO_Pin = GPIO_Pin_10;
    gpio_uart_init_s.GPIO_Mode = GPIO_Mode_FLOATING;
    GPIO_Init(GPIOA, &gpio_uart_init_s);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
    //-TKW24-0715-USART Config
    UART_InitTypeDef uart_init_s;
    uart_init_s.BaudRate = 115200;
    uart_init_s.WordLength = UART_WordLength_8b;
    uart_init_s.StopBits = UART_StopBits_1;
    uart_init_s.Parity = UART_Parity_No;
    uart_init_s.HWFlowControl = UART_HWFlowControl_None;
    uart_init_s.Mode = UART_Mode_Rx | UART_Mode_Tx;
    UART_Init(UART1, &uart_init_s);
    UART_Cmd(UART1, ENABLE);
    //-TKW24-0715-Uart Interrupt Config
    UART_ITConfig(UART1, UART_IT_RX, ENABLE);
    UART_ITConfig(UART1, UART_IT_RXIDLE, ENABLE);
    NVIC_SetPriority(UART1_IRQn, 0);
    NVIC_EnableIRQ(UART1_IRQn);

    return 0;
}

uint8_t    UartComPrintf(char *fmt,...)
{
    va_list arg_ptr;
    uint8_t len = 0;
	uint8_t i = 0;
    char str[0XFF];

    va_start(arg_ptr, fmt);
    len = vsprintf(str, fmt, arg_ptr);
    va_end(arg_ptr);
	
    for (i=0; i<len; i++)
    {
        UART_SendData(UART1, str[i]);
        while (UART_GetFlagStatus(UART1, UART_FLAG_TXEPT) == RESET);
    }
    
    return 0;
}

volatile uint8_t    uart_com_data[0XFF];
volatile uint8_t    uart_com_data_len = 0;
volatile uint8_t    uart_com_flag = 0;
void UART1_IRQHandler(void)
{
    uint8_t ch;
    if (UART_GetITStatus(UART1, UART_IT_RX) != RESET)
    {   //-TKW24-0715-Uart Rx
        UART_ClearITPendingBit(UART1,UART_IT_RX);
        uart_com_data[uart_com_data_len] = UART_ReceiveData(UART1);
        uart_com_data_len++;

        // UART_SendData(UART1, ch);
        // while (UART_GetFlagStatus(UART1, UART_FLAG_TXEPT) == RESET);
    }

    if (UART_GetITStatus(UART1, UART_IT_RXIDLE) != RESET)
    {   //-TKW24-0715-Uart Rx Idle
        UART_ClearITPendingBit(UART1,UART_IT_RXIDLE);
        uart_com_data[uart_com_data_len] = '\0';
        uart_com_flag = 1;
        // uart_com_data[uart_com_data_len] = '\0';
        // uart_com_data_len = 0;
        // UartComPrintf("%s", uart_com_data);
    }

}   



/********************************End of File************************************/

